// pages/usedCar/usedCar.js
const app = getApp();
const constant = require('../../constant');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    count: 0,
    username: '',
    phone: '',
    code: '',
    agreed: false,
    image01: '',
    image02: ''
  },

  getCount: function() {
    let that = this;
    app.get('Goods/getregsaleinfocount', {
    }, function (res) {
      if (res.code == 200) {
        that.setData({
          count: res.data
        })
      }
    })
  },
  inputUsername: function(e) {
    this.setData({
      username: e.detail.value
    })
  },
  inputPhone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputPhoneCode: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  checkboxchange: function(e) {
    var isAgree = false;
    if (e.detail.value.length == 1) {
      isAgree = true;
    }
    this.setData({ agreed: isAgree});
  },
  sendSms: function() {
    app.get('User/sendsmsafterlogin', {
      phone: this.data.phone
    }, function (res) {
      wx.showToast({
        title: res.message, icon: 'none', duration: 500
      })
    })
  },
  displayProtocol: function (e) {
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../protocolDisplay/protocolDisplay?type=' + type
    })
  },
  toSale: function() {
    let that = this;
    if (this.data.agreed == false) {
      wx.showModal({
        title: '提示',
        content: '请阅读并勾选卖车协议',
      })
      return;
    }
    app.get('User/regsaleinfo', {
      username: that.data.username,
      phone: that.data.phone,
      code: that.data.code
    }, function (res) {
      wx.showToast({
        title: res.message,
        duration: 500,
        success(){
          setTimeout(function(){
            wx.navigateBack({
              delta: 1
            })
          }, 500)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      image01: constant.config.url_public + 'usedCar_01.jpg',
      image02: constant.config.url_public + 'usedCar_02.jpg',
    })
    this.getCount();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})